﻿using System;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using LmgCMS.Service;
using LmgCMS.Model;

namespace LmgCMS
{
    public static class BusinessStartup
    {
        public static void AddBusinessServices(this IServiceCollection services, IConfiguration configuration)
        {
            //仓储模式注入
            services.AddSingleton<ILoginInfo, LoginInfo>();
            services.AddSingleton<IRelevanceService, RelevanceService>();
            services.AddSingleton<ISysLogService, SysLogService>();
            services.AddSingleton<IOpenJobService, OpenJobService>();
       
            //content
            services.AddSingleton<ISiteService, SiteService>();
            services.AddSingleton<ICommentService, CommentService>();
            services.AddSingleton<IFriendlinkService, FriendlinkService>();
            services.AddSingleton<INewsService, NewsService>();
            services.AddSingleton<INewsDataService, NewsDataService>();
            //rbac
            services.AddSingleton<IMenuElementService, MenuElementService>();
            services.AddSingleton<IMenuService, MenuService>();
            services.AddSingleton<IOrganizeService, OrganizeService>();
            services.AddSingleton<IRoleService, RoleService>();
            services.AddSingleton<IUserOnlineService, UserOnlineService>();
            services.AddSingleton<IUserService, UserService>();
            //setting
            services.AddSingleton<ISettingService, SettingService>();
            //register all settings
            var typeFinder = new WebAppTypeFinder();
            var settings = typeFinder.FindClassesOfType(typeof(ISettings), false).ToList();
            foreach (var setting in settings)
            {
                services.AddScoped(setting, serviceProvider => {
                    var storeId = 0;
                    return serviceProvider.GetRequiredService<ISettingService>().LoadSetting(setting, storeId);
                });
            }
        }
    }
}
